/**
 * Copyright &copy; 2015-2020 <a href="http://www.gx-soft.cn/">GxSoft</a> All rights reserved.
 */
package com.gxsoft.coll.dateutils;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
 * @author gxsoft
 * @version 2017-4-15
 */
public class DateUtils  {

	private final static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
	private final static SimpleDateFormat sdfMonth = new SimpleDateFormat("MM");
	private final static SimpleDateFormat sdfDay = new SimpleDateFormat("dd");
	private final static SimpleDateFormat sdfHH = new SimpleDateFormat("HH");
	private final static SimpleDateFormat sdfmm = new SimpleDateFormat("mm");
	private final static SimpleDateFormat sdfss = new SimpleDateFormat("ss");

	private final static SimpleDateFormat sdfDayDate = new SimpleDateFormat("yyyy-MM-dd");

	private final static SimpleDateFormat sdfDays = new SimpleDateFormat("yyyyMMdd");

	private final static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	private final static SimpleDateFormat DateTime = new SimpleDateFormat("HHmmss");



	/**
	 * 得到当前年份字符串 格式（yyyy）
	 */
//	public static String getYear() {
//		return formatDate(new Date(), "yyyy");
//	}

	/**
	 * 得到当前月份字符串 格式（MM）
	 */
	public static String getMonth(Date date) {
		return sdfMonth.format(date);
	}

	/**
	 * 得到当天字符串 格式（dd）
	 */
	public static String getDay(Date date) {
		return sdfDay.format(date);
	}

	/**
	 * 得到当天小时字符串 格式（HH）
	 */
	public static String getHH(Date date) {
		return sdfHH.format(date);
	}
	/**
	 * 得到当天分钟字符串 格式（ss）
	 */
	public static String getmm(Date date) {
		return sdfmm.format(date);
	}

	/**
	 * 得到当天秒字符串 格式（ss）
	 */
	public static String getss(Date date) {
		return sdfss.format(date);
	}

	/**
	 * 获取YYYY格式
	 * @return
	 */
	public static String getYear(Date date) {
		return sdfYear.format(date);
	}

	/**
	 * 获取YYYY-MM-DD格式
	 * @return
	 */
	public static String getDayDate() {
		return sdfDayDate.format(new Date());
	}

	/**
	 * 获取YYYYMMDD格式
	 * @return
	 */
	public static String getDays(Date date) {
		return sdfDays.format(new Date());
	}

	/**
	 * 获取YYYY-MM-DD HH:mm:ss格式
	 * @return
	 */
	public static String getTime() {
		return sdfTime.format(new Date());
	}
	public static String getDateTime() {
		return DateTime.format(new Date());
	}
	/**
	 * @Title: compareDate
	 * @Description: TODO(日期比较，如果s>=e 返回true 否则返回false)
	 * @param s
	 * @param e
	 * @return boolean
	 * @throws
	 * @author luguosui
	 */
	public static boolean compareDate(String s, String e) {
		if (fomatDate(s) == null || fomatDate(e) == null) {
			return false;
		}
		return fomatDate(s).getTime() >= fomatDate(e).getTime();
	}

	/**
	 * 格式化日期
	 * @param date yyyy-MM-dd"
	 * @return
	 */
	public static Date fomatDate(String date) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return fmt.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 日期转字符串
	 * @param date
	 * @param fomat  yyyy-MM-dd
	 * @return
	 */
	public static String dateToString(Date date,String fomat) {
		DateFormat fmt = null;
		if(fomat == null){
			fmt = new SimpleDateFormat("yyyy-MM-dd");
		}else {
			fmt = new SimpleDateFormat(fomat);
		}
		return fmt.format(date);
	}
	/**
	 * 格式化日期(精确到秒)
	 * @return
	 */
	public static Date fomatDate2(String date) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return fmt.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 格式化日期
	 * yyyy-MM-dd HH:mm:ss
	 * @return String
	 */
	public static String fomatDate3(Date date) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr ="";
		try {
			dateStr=df.format(date);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return dateStr;
	}

	/**
	 * 校验日期是否合法
	 * @return
	 */
	public static boolean isValidDate(String s) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		try {
			fmt.parse(s);
			return true;
		} catch (Exception e) {
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			return false;
		}
	}

	public static int getDiffYear(String startTime, String endTime) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		try {
			long aa = 0;
			int years = (int) (((fmt.parse(endTime).getTime() - fmt.parse(startTime).getTime()) / (1000 * 60 * 60 * 24)) / 365);
			return years;
		} catch (Exception e) {
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			return 0;
		}
	}

	/**
	 * <li>功能描述：时间相减得到天数
	 * @param beginDateStr
	 * @param endDateStr
	 * @return long
	 * @author Administrator
	 */
	public static long getDaySub(String beginDateStr, String endDateStr) {
		long day = 0;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date beginDate = null;
		Date endDate = null;

		try {
			beginDate = format.parse(beginDateStr);
			endDate = format.parse(endDateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
		// System.out.println("相隔的天数="+day);

		return day;
	}

	/**
	 * 得到n天之后的日期
	 * @param days
	 * @return
	 */
	public static String getAfterDayDate(String days) {
		int daysInt = Integer.parseInt(days);

		Calendar canlendar = Calendar.getInstance(); // java.util包
		canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
		Date date = canlendar.getTime();

		SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = sdfd.format(date);

		return dateStr;
	}

	/**
	 * 得到n天之后的日期(日期格式)
	 * @param days
	 * @return
	 */
	public static Date getAfterDayDate2(String days) {
		int daysInt = Integer.parseInt(days);

		Calendar canlendar = Calendar.getInstance(); // java.util包
		canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
		Date date = canlendar.getTime();

		return date;
	}

	/**
	 *得到n月之后的日期
	 * @param months
	 * @param date
	 * @return
	 */
	public static Date getDayAfterMonth(int months,Date date)
	{
		if(date == null) {
			date = new Date(System.currentTimeMillis());
		}
		Calendar gc = (Calendar)Calendar.getInstance();
		gc.setTime(date);
		gc.add(Calendar.MONTH, months);
		return gc.getTime();
	}

	/**
	 *得到n天之后的日期,如果date为null ，则以当前系统时间开始
	 * @param days
	 * @param date
	 * @return
	 */
	public static Date getDateAfterDays(int days,Date date)
	{
		if(date == null) {
			date = new Date(System.currentTimeMillis());
		}
		Calendar gc = (Calendar)Calendar.getInstance();
		gc.setTime(date);
		gc.add(Calendar.DATE, days);
		return gc.getTime();
	}
	public static Date getDateOfZeroPoint(Date date)
	{
		Calendar gc = (Calendar)Calendar.getInstance();
		gc.setTime(date);
		gc.set(Calendar.HOUR_OF_DAY, 0);
		gc.set(Calendar.MINUTE, 0);
		gc.set(Calendar.SECOND, 0);
		return gc.getTime();
	}
	public static Date getDateOf23_59_59(Date date)
	{
		Calendar gc = (Calendar)Calendar.getInstance();
		gc.setTime(date);
		gc.set(Calendar.HOUR_OF_DAY, 23);
		gc.set(Calendar.MINUTE, 59);
		gc.set(Calendar.SECOND, 59);
		return gc.getTime();
	}
	/**
	 * 得到n天之后是周几
	 * @param days
	 * @return
	 */
	public static String getAfterDayWeek(String days) {
		int daysInt = Integer.parseInt(days);

		Calendar canlendar = Calendar.getInstance(); // java.util包
		canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
		Date date = canlendar.getTime();

		SimpleDateFormat sdf = new SimpleDateFormat("E");
		String dateStr = sdf.format(date);

		return dateStr;
	}
	/**
	 * 获取指定日期当月的第一天
	 *
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static String getFirstDayOfGivenMonth(String dateStr,String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		if(dateStr == null){
			dateStr = getDayDate();
			sdf = sdfDay;
		}

		try {
			Date date = sdf.parse(dateStr);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.set(Calendar.DAY_OF_MONTH,1);
			calendar.add(Calendar.MONTH, 0);
			return sdf.format(calendar.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}


	/**
	 * @Description: 获取当前时间上1个小时
	 */
	public static String beforeOneHourToNowDate() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(calendar.getTime());
	}

	/**获取上n个小时整点小时时间
	 * @param date
	 * @return
	 */
	public static Date getLastHourTime(Date date,int n){
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.MINUTE, 0);
		ca.set(Calendar.SECOND, 0);
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ca.set(Calendar.HOUR_OF_DAY, ca.get(Calendar.HOUR_OF_DAY)-n);
		date = ca.getTime();
//		return sdf.format(date);
		return date;
	}

	public static String DateToStringBeginOrEnd(Date date, Boolean flag) {
		String time = null;
		SimpleDateFormat dateformat1 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		Calendar calendar1 = Calendar.getInstance();
		//获取某一天的0点0分0秒 或者 23点59分59秒
		if (flag) {
			calendar1.setTime(date);
			calendar1.set(calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH), calendar1.get(Calendar.DAY_OF_MONTH),
					0, 0, 0);
			Date beginOfDate = calendar1.getTime();
			time = dateformat1.format(beginOfDate);
			System.out.println(time);
		}else{
			Calendar calendar2 = Calendar.getInstance();
			calendar2.setTime(date);
			calendar1.set(calendar2.get(Calendar.YEAR), calendar2.get(Calendar.MONTH), calendar2.get(Calendar.DAY_OF_MONTH),
					23, 59, 59);
			Date endOfDate = calendar1.getTime();
			time = dateformat1.format(endOfDate);
			System.out.println(time);
		}
		return time;
	}



	/**
	 * 获取指定日期下个月的第一天
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static String getFirstDayOfNextMonth(String dateStr,String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		if(dateStr == null){
			dateStr = getDayDate();
			sdf = sdfDay;
		}

		try {
			Date date = sdf.parse(dateStr);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.set(Calendar.DAY_OF_MONTH,1);
			calendar.add(Calendar.MONTH, 1);
			return sdf.format(calendar.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date getNextMonthFirstDay(Date date)
	{
		Calendar gc = (Calendar)Calendar.getInstance();
		gc.setTime(date);
		gc.add(Calendar.MONTH, 1);
		gc.set(Calendar.DATE, 1);

		gc.set(Calendar.HOUR_OF_DAY, 0);
		gc.set(Calendar.MINUTE, 0);
		gc.set(Calendar.SECOND, 0);
		return gc.getTime();
	}
	public static Date getMonthFirstDay(Date date)
	{
		Calendar gc = (Calendar)Calendar.getInstance();
		gc.setTime(date);
		gc.set(Calendar.DATE, 1);

		gc.set(Calendar.HOUR_OF_DAY, 0);
		gc.set(Calendar.MINUTE, 0);
		gc.set(Calendar.SECOND, 0);
		return gc.getTime();
	}
	public static void main(String[] args) {


//		long time1 = System.currentTimeMillis();
//		long offset = 1000;
//		long time2 = time1 + offset;
//		System.out.println(String.valueOf(time1));
//		System.out.println(time2);
//		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
//
//
//
//		Calendar cal1 = Calendar.getInstance();
//		cal1.setTimeInMillis(time1);
//		System.out.println(time1 + " is " + format.format(cal1.getTime()));
//
//
//		Calendar cal = Calendar.getInstance();
//		cal.setTimeInMillis(time2);
//		System.out.println(time2 + " is " + format.format(cal.getTime()));
		System.out.println(DateUtils.getOneDayBefore(new Date()));


	}

//	获取某时刻过去的24小时
	public static Date getOneDayBefore(Date dateEnd){
		Calendar date = Calendar.getInstance();
		date.setTime(dateEnd);
		date.set(Calendar.DATE, date.get(Calendar.DATE) - 1);
		return date.getTime();
	}

}
